﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using RayD.Core;
using RayD.Repository.Entites.SystemModule;
using RayD.App.SystemModule;
using RayD.WebApi.Models;
using RayD.Repository;

namespace RayD.WebApi.Controllers.Admin
{
    /// <summary>
    /// 用户关系资源
    /// </summary>
    public class UserRelationController : BaseAdminController
    {
        #region 构造函数注入
        private UserRelationApp _UserRelation;
        public UserRelationController(UserRelationApp UserRelation)
        {
            this._UserRelation = UserRelation;
        }
        #endregion

        #region 参数实体
        /// <summary>
        /// 获取对象主键列表响应参数
        /// </summary>
        public class GetObjIdRespone
        {
            /// <summary>
            /// 角色Id集合
            /// </summary>
            public List<string> roleIdList { get; set; }
            /// <summary>
            /// 岗位Id集合
            /// </summary>
            public List<string> postIdList { get; set; }
        }
        /// <summary>
        /// 保存用户对应对象数据请求数据
        /// </summary>
        public class SaveFormRequest
        {
            /// <summary>
            /// 分类:1-角色2-岗位
            /// </summary>
            public int category { get; set; }
            /// <summary>
            /// 对应对象主键
            /// </summary>
            public string objectId { get; set; }
            /// <summary>
            /// 对应用户主键列表
            /// </summary>
            public List<string> userIds { get; set; }
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取用户主键列表信息
        /// </summary>
        /// <param name="objectId">对象主键</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<List<string>> GetUserIdList(string objectId)
        {
            var list = _UserRelation.GetUserIdList(objectId);
            return Success(list);
        }
        /// <summary>
        /// 获取对象主键列表信息
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<GetObjIdRespone> GetObjIdList(string userId)
        {
            var roleIdList = _UserRelation.GetObjIdList(userId, "1");
            var postIdList = _UserRelation.GetObjIdList(userId, "2");
            return Success(new GetObjIdRespone()
            {
                postIdList = postIdList,
                roleIdList = roleIdList
            });
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 保存用户对应对象数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut, Logging("用户关系保存")]
        public ResParameter SaveForm(SaveFormRequest request)
        {
            var (isSuccess, msg) = _UserRelation.SaveForm(request.category, request.objectId, request.userIds);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        #endregion
    }
}
